<?php

/**
 * This is the model class for table "setting".
 *
 * The followings are the available columns in table 'setting':
 * @property integer $setting_id
 * @property integer $store_id
 * @property string $group
 * @property string $key
 * @property string $value
 * @property integer $serialized
 * config_meta_description - Описание в теге "мета дескриптион"
 * config_error_filename - файл ошибок
 * config_error_display - показывать ли ошибки
 * config_error_log - записывать ли ошибки
 * config_language - Язык по умолчанию
 */
class Setting extends CActiveRecord
{
	public function tableName()
	{
		return 'setting';
	}

	public function rules()
	{
		return array(
			array('group, value, serialized', 'required'),
			array('store_id, serialized', 'numerical', 'integerOnly'=>true),
			array('group', 'length', 'max'=>32),
			array('key', 'length', 'max'=>64),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('setting_id, store_id, group, key, value, serialized', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'setting_id' => 'Setting',
			'store_id' => 'Store',
			'group' => 'Group',
			'key' => 'Key',
			'value' => 'Value',
			'serialized' => 'Serialized',
		);
	}

    public static function getConfig($configName)
    {
        $result=self::model()->findByAttributes(array('key'=>$configName));
        if($result && isset($result->value)) return $result->value;
        else{
            return false;
        }
    }

    public function getNames()
    {
        $result=array();
        $names=$this->findAll();
        if($names){
            foreach($names as $name){
                if(isset($name->key)) $result[]=$name->key;
            }
        }else{
            return false;
        }
        if($result) return $result;
        else{
            return false;
        }
    }

    public function getAll()
    {
        $result=array();
        $names=$this->findAll();
        if($names){
            foreach($names as $name){
                if(isset($name->key) && isset($name->value)) $result[$name->key]=$name->value;
            }
        }else{
            return false;
        }
        if($result) return $result;
        else{
            return false;
        }
    }

	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('setting_id',$this->setting_id);
		$criteria->compare('store_id',$this->store_id);
		$criteria->compare('group',$this->group,true);
		$criteria->compare('key',$this->key,true);
		$criteria->compare('value',$this->value,true);
		$criteria->compare('serialized',$this->serialized);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
